# common target bin
# Created by tongzhilin on 2020.08.05.

#字体颜色 重置=0，黑色=30，红色=31，绿色=32， 黄色=33，蓝色=34， 洋红（粉）=35， 青色=36， 白色=37
#背景颜色 重置=0，黑色=40，红色=41，绿色=42， 黄色=43，蓝色=44， 洋红（粉）=45， 青色=46， 白色=47

SRC  := $(wildcard *.c)
OBJ  := $(SRC:%.c=%.o)
DEPS := $(SRC:%.c=%.o.d)

CPPSRC  := $(wildcard *.cpp)
CPPOBJ  := $(CPPSRC:%.cpp=%.o)
DEPS    += $(CPPSRC:%.cpp=%.o.d)

CXXFLAGS += $(INC_FLAGS)
CFLAGS += $(INC_FLAGS)
#CFLAGS += -Wl,-Map,$(TARGET).map 

STDCPP_FLAG = $(findstring stdc++,$(CFLAGS))
ifeq ($(STDCPP_FLAG), stdc++)
	LINKING = $(CC)
else
	LINKING = $(CPP)
endif

all: $(TARGET)

$(TARGET):$(OBJ) $(CPPOBJ)
	@echo -e "\e[1;32mTARGET Linking bin $@ start \e[0m "
	$(PR)$(LINKING) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(TARGET_DEPEND_A) -Wl,--start-group $(LDLIBS) -Wl,--end-group
	@echo -e "\e[1;32mTARGET Linking bin $@ end \e[0m "
ifeq ($(COMPILE_RELEASE),y)
	$(PR)$(CROSS)strip $(TARGET)
	@echo "strip $(TARGET)"
endif
	$(PR)mv $(TARGET) $(OUTPUT_BIN_DIR)

$(OBJ): $ %.o: %.c
	$(PR)$(CC) -c $< -o $@ $(CFLAGS) -MD -MF "$(dir $@)$(notdir $@).d" -MT "$@"
	@echo CC $@

$(CPPOBJ): $ %.o: %.cpp
	$(PR)$(CPP) -c $< -o $@ $(CXXFLAGS) -MD -MF "$(dir $@)$(notdir $@).d" -MT "$@"
	@echo CXX $@

clean:
	$(PR)rm -f $(TARGET)
	$(PR)rm -f $(OBJ) $(CPPOBJ) $(DEPS)

.PHONY : clean all

-include $(DEPS)
